package io.xxx.omni.oss.custom

import com.google.gson.JsonObject
import io.xxx.omni.oss.core.PageSynchronizer
import io.xxx.omni.oss.sdk.WphClient

/**
 * 唯品会
 */
// https://vop.vip.com/doccenter/viewdoc/8
abstract class WphSynchronizer : PageSynchronizer<JsonObject>() {

    protected abstract val method: String

    override val pageSize = 200

    override fun getResponse(context: Context, pageNo: Int, containsTotal: Boolean): JsonObject {
        val request = buildRequest(context, pageNo)
        val client = applicationContext.getBean(WphClient::class.java)
        val response = client.execute(store, method, request)
        if (response["returnCode"].asInt != 0) {
            throwException(context, request, response)
        }
        return response
    }

    override fun getCount(context: Context, response: JsonObject): Int {
        return response.getAsJsonObject("result")["total"].asInt
    }

    protected abstract fun buildRequest(context: Context, pageNo: Int): Map<String, Any>
}